#!/bin/sh

#set -e

# Source debconf library.
. /usr/share/debconf/confmodule

DB_USER='nipap'
DB_PASS=`</dev/urandom tr -dc [:alpha:] | head -c16`
DB_NAME='nipap'

POSTGRES_INSTALLED=0
POSTGRES_RUNNING=0
NIPAP_DB_EXISTS=0

# determine if postgres is installed
# TODO: even with a local database we don't know if we have psql since nipapd
#		doesn't depend on the postgresql-client package
su postgres -c 'psql --version' > /dev/null 2>&1
if [ $? -eq 0 ]; then
	POSTGRES_INSTALLED=1

	su postgres -c "psql -c '\l+'" >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		POSTGRES_RUNNING=1
		# does the NIPAP database exist?
		if [ "`su postgres -c "psql -c '\l+'" | awk -F"|" '{ if ($1~/^ *nipap *$/) { gsub(/^ */, "", $1); gsub(/ *$/, "", $1); print $1 } }'`" = "nipap" ]; then
			NIPAP_DB_EXISTS=1
		fi
	fi
fi

if [ -e "/etc/nipap/nipap.conf" ]; then
	# recover password if we already have one
	CONF_DB_PASS=`awk -F'=' '/db_pass/ { print $2 }' /etc/nipap/nipap.conf | awk '{ print $1 }'`
	if [ -n "$CONF_DB_PASS" -a "$CONF_DB_PASS" != "papin" ]; then
		DB_PASS=$CONF_DB_PASS
	fi
fi

# get database host
db_get nipapd/database_host
DB_HOST=$RET

case "$1" in
	configure)
		if [ ! -e "/etc/nipap/nipap.conf" ]; then
			# update nipap.conf with database credentials
			cat /etc/nipap/nipap.conf.dist | sed -e "s/#user *= *[^ ]\+/user = nipap/" -e "s/{{LISTEN_ADDRESS}}/127.0.0.1/" -e "s/{{LISTEN_PORT}}/1337/" -e "s/{{DB_USERNAME}}/$DB_USER/" -e "s/{{DB_NAME}}/$DB_NAME/" -e "s/{{DB_PASSWORD}}/$DB_PASS/" -e "s/{{DB_SSLMODE}}/require/" -e "s/{{DB_PORT}}/5432/" -e "s/{{SYSLOG}}/true/" > /etc/nipap/nipap.conf
		fi
		# fill in db_host
		if [ $DB_HOST = "localhost" ]; then
			sed -e 's/{{DB_HOST}}//' -i /etc/nipap/nipap.conf
		else
			sed -e "s/{{DB_HOST}}/$DB_HOST/" -i /etc/nipap/nipap.conf
		fi

		# create group / user for nipapd
		if [ -z "`getent passwd nipap`" ]; then
			adduser --quiet --system --group --no-create-home nipap
		fi

		# autostart
		db_get nipapd/startup
		if [ "$RET" = "true" ]; then
			sed -i -e 's/RUN=.*/RUN=yes/' /etc/default/nipapd
			update-rc.d nipapd defaults >/dev/null 2>&1
		else
			sed -i -e 's/RUN=.*/RUN=no/' /etc/default/nipapd
			update-rc.d nipapd remove >/dev/null 2>&1
		fi

		# local or remote db?
		if [ "$DB_HOST" = "localhost" ]; then
			# automatically do install?
			db_get nipapd/local_db_autoconf
			if [ "$RET" = "true" ]; then
				sed -i -e 's/AUTO_INSTALL=.*/AUTO_INSTALL=yes/' /etc/default/nipapd
			else
				sed -i -e 's/AUTO_INSTALL=.*/AUTO_INSTALL=no/' /etc/default/nipapd
			fi

			# automatically do upgrades?
			db_get nipapd/local_db_upgrade
			if [ "$RET" = "true" ]; then
				sed -i -e 's/AUTO_UPGRADE=.*/AUTO_UPGRADE=yes/' /etc/default/nipapd
			else
				sed -i -e 's/AUTO_UPGRADE=.*/AUTO_UPGRADE=no/' /etc/default/nipapd
			fi

		else
			# automatically do install?
			db_get nipapd/remote_db_install
			if [ "$RET" = "true" ]; then
				sed -i -e 's/AUTO_INSTALL=.*/AUTO_INSTALL=yes/' /etc/default/nipapd
			else
				sed -i -e 's/AUTO_INSTALL=.*/AUTO_INSTALL=no/' /etc/default/nipapd
			fi

			# automatically do upgrade?
			db_get nipapd/remote_db_upgrade
			if [ "$RET" = "true" ]; then
				sed -i -e 's/AUTO_UPGRADE=.*/AUTO_UPGRADE=yes/' /etc/default/nipapd
			else
				sed -i -e 's/AUTO_UPGRADE=.*/AUTO_UPGRADE=no/' /etc/default/nipapd
			fi
		fi


		# if database isn't running
		if [ $POSTGRES_RUNNING -eq 0 ]; then
			# are we asked to automagically install?
			db_get nipapd/local_db_autoconf
			if [ "$RET" = "true" ]; then
				echo "PostgreSQL is not running (or it's running on another machine). Either start postgres locally or if postgres is running on another machine you will need to create the user and database manually." >&2
			fi

		else # db is running, let's load some db files or upgrade!
			if [ $NIPAP_DB_EXISTS -eq 0 ]; then # no db, ie first time install!
				# first time setup
				db_get nipapd/local_db_autoconf
				if [ "$RET" = "true" ]; then
					# setup new user
					su -c "createuser -S -D -R -w $DB_USER" postgres >/dev/null 2>&1
					su -c "psql -q -c \"ALTER USER $DB_USER ENCRYPTED PASSWORD '$DB_PASS'\"" postgres >/dev/null 2>&1
					# create database and install plpgsql
					su -c "createdb -O $DB_USER $DB_NAME" postgres >/dev/null 2>&1
					su -c "psql -q -d $DB_NAME -c \"CREATE EXTENSION citext\"" postgres >/dev/null 2>&1
					su -c "psql -q -d $DB_NAME -c \"CREATE EXTENSION hstore\"" postgres >/dev/null 2>&1
					su -c "psql -q -d $DB_NAME -c \"CREATE EXTENSION ip4r\"" postgres >/dev/null 2>&1
				fi
			fi
		fi

		# create local auth database
		nipap-passwd create-database > /dev/null 2>&1

		# set ownership/permissions on the local auth db
		chown -f :nipap /etc/nipap/local_auth.db
		chmod -f 0644 /etc/nipap/local_auth.db

		# upgrade sqlite?
		db_get nipapd/sqlite_upgrade
		if [ "$RET" = "true" ]; then
			nipap-passwd upgrade-database > /dev/null 2>&1
		fi

		db_stop

		# finally start nipapd
		invoke-rc.d nipapd start
		;;
esac

exit 0
